package com.ruoyi.system.service.impl;


import com.ruoyi.common.constant.AnythingllmConstant;
import com.ruoyi.system.domain.ChatRequest;
import com.ruoyi.system.domain.ChatResponse;
import com.ruoyi.system.domain.RemoteChatRequest;
import com.ruoyi.system.domain.WorkspaceGetResponse;
import com.ruoyi.system.service.WorkspaceService;
import org.springframework.beans.BeanUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private AnythingllmConstant anythingllmConstant;


    @Override
    public ResponseEntity<ChatResponse> getChat(ChatRequest chatRequest) {
        RemoteChatRequest remoteChatRequest = new RemoteChatRequest();
        BeanUtils.copyProperties(chatRequest, remoteChatRequest);
        String workspace = chatRequest.getWorkspace();

        HttpEntity<RemoteChatRequest> entity = new HttpEntity<>(remoteChatRequest);

        return restTemplate.exchange(
                anythingllmConstant.workspaceUrl + "/" + workspace + "/chat",
                HttpMethod.POST,
                entity,
                ChatResponse.class
        );
    }

    @Override
    public WorkspaceGetResponse getWorkspaceByName(String workspaceName) {

        ResponseEntity<WorkspaceGetResponse> response = restTemplate.exchange(
                anythingllmConstant.workspaceUrl + "/" + workspaceName,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<WorkspaceGetResponse>() {
                }
        );

        return response.getBody();
    }
}




